<template>
    <div class="system-offer-container layout-padding">
        <el-card shadow="hover" class="layout-padding-auto">
            <el-card shadow="hover" class="layout-padding-auto">
                <div class="table-search-container">
                    <el-form ref="tableSearchRef" :model="state.ruleForm" size="default" label-width="120px"
                        class="table-form">
                        <el-row>





                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20">
                                <el-form-item label="用户地址">
                                    <el-input v-model="state.ruleForm.fromAddress" style="width: 100%"
                                        placeholder="用户地址"></el-input>
                                </el-form-item>
                            </el-col>



                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20" v-show="state.isToggle">
                                <el-form-item label="接收地址">
                                    <el-input v-model="state.ruleForm.toAddress" style="width: 100%"
                                        placeholder="接收地址"></el-input>
                                </el-form-item>
                            </el-col>





                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20" v-show="state.isToggle">
                                <el-form-item label="币种">
                                    <el-select v-model="state.ruleForm.coinId" placeholder="请选择" clearable
                                        style="width: 100%">
                                        <el-option v-for="i in state.coinList" :key="i.coinId" :value="i.coinId"
                                            :label="i.coinSymbol">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>





                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20" v-show="state.isToggle">
                                <el-form-item label="创建时间">
                                    <el-date-picker v-model="state.ruleForm.createDate" type="datetimerange"
                                        :picker-options="state.pickerOptions" range-separator="至" start-placeholder="开始日期"
                                        end-placeholder="结束日期" align="right">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>





                            <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                                <el-form-item class="table-form-btn" :label-width="!state.isToggle ? '10px' : '35px'">
                                    <div class="table-form-btn-toggle ml10" @click="state.isToggle = !state.isToggle">
                                        <span>{{ state.isToggle ? '收筛选' : '展筛选' }}</span>
                                        <SvgIcon :name="state.isToggle ? 'ele-ArrowUp' : 'ele-ArrowDown'" />
                                    </div>
                                    <div>
                                        <el-button size="default" type="primary" @click="refresh()">查询
                                        </el-button>
                                        <el-button size="default" type="info" class="ml10" @click="handleResetSearch()"> 重置
                                        </el-button>
                                    </div>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </div>





                <el-table size="default" :data="state.dataList" style="width: 100%" border>

                    <el-table-column align="center" width="80" label="ID">
                        <template #default="scope">
                            <span>{{ scope.row.id }}</span>
                        </template>
                    </el-table-column>


                    <el-table-column align="center" label="操作地址" width="200">
                        <template #default="scope">
                            <span>{{ scope.row.fromAddress }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="接收地址" width="200">
                        <template #default="scope">
                            <span>{{ scope.row.toAddress }}</span>
                        </template>
                    </el-table-column>




                    <el-table-column align="center" label="币种" width="160">
                        <template #default="scope">
                            <span>{{ scope.row.coinSymbol }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="币种Id" width="160">
                        <template #default="scope">
                            <span>{{ scope.row.coinId }}</span>
                        </template>
                    </el-table-column>



                    <el-table-column align="center" label="操作金额" width="160">
                        <template #default="scope">
                            <span>{{ scope.row.amount }} {{ scope.row.coinSymbol }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="手续费" width="160">
                        <template #default="scope">
                            <span>{{ scope.row.feeAmount }} {{ scope.row.coinSymbol }}</span>
                        </template>
                    </el-table-column>



                    <el-table-column align="center" label="创建时间" width="160">
                        <template #default="scope">
                            <span>{{ formatDate2(scope.row.createdDate) }}</span>
                        </template>
                    </el-table-column>



                </el-table>

            </el-card>

            <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
                :pager-count="5" :page-sizes="[20, 50, 100]" v-model:current-page="state.page" background
                v-model:page-size="state.limit" layout="total, sizes, prev, pager, next, jumper" :total="state.total">
            </el-pagination>

        </el-card>

    </div>
</template>


<script setup lang="ts" name="systemMenu">


import { formatDate } from '/@/utils/formatTime';
import { onMounted, reactive, defineAsyncComponent, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';


import { useFundApi } from '/@/api/fund/index';







const state = reactive({
    isToggle: false,
    page: 1,
    limit: 20,
    total: 0,
    typeList: [] as any,
    coinList: [] as any,
    levelList: [] as any,
    dataList: [] as any,
    sorts: {} as any,
    ruleForm: {
        coinId: "",
        createDate: [],
        fromAddress: "",
        toAddress: ""
    },
    pickerOptions: {
        shortcuts: [
            {
                text: "最近一周",
                onClick(picker: any) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(
                        start.getTime() - 3600 * 1000 * 24 * 7
                    );
                    picker.$emit("pick", [start, end]);
                },
            },
            {
                text: "最近一个月",
                onClick(picker: any) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(
                        start.getTime() - 3600 * 1000 * 24 * 30
                    );
                    picker.$emit("pick", [start, end]);
                },
            },
            {
                text: "最近三个月",
                onClick(picker: any) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(
                        start.getTime() - 3600 * 1000 * 24 * 90
                    );
                    picker.$emit("pick", [start, end]);
                },
            },
        ],
    }
});



const handleResetSearch = () => {
    state.ruleForm = {
        coinId: "",
        createDate: [],
        fromAddress: "",
        toAddress: ""
    };
    state.page = 1;
    state.limit = 20;
    adminGetTransferHis();
}


const formatDate2 = (str: any) => {
    if (!str) return "-";
    return formatDate(new Date(str), "YYYY-mm-dd HH:MM")
}


// 分页改变
const onHandleSizeChange = (val: number) => {
    state.limit = val;
    adminGetTransferHis();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
    state.page = val;
    adminGetTransferHis();
};



const refresh = () => {
    adminGetTransferHis();
}


const getHash = (item: any) => {

    if (item.chainId == '888') {
        return `https://app.lianziyou.tech:4000/tx/${item.transactionHash}`;
    } else if (item.chainId == '56') {
        return `https://bscscan.com/tx/${item.transactionHash}`;
    } else {
        return `https://etherscan.io/tx/${item.transactionHash}`;
    }


}




const adminGetTransferHis = async () => {
    let vo = {
        page: state.page,
        limit: state.limit
    } as any;


    if (state.ruleForm.fromAddress) vo['fromAddress'] = state.ruleForm.fromAddress;
    if (state.ruleForm.toAddress) vo['toAddress'] = state.ruleForm.toAddress;


    if (state.ruleForm.coinId) vo['coinId'] = state.ruleForm.coinId;


    if (state.ruleForm.createDate.length > 0) {
        vo["createTime"] = [formatDate(new Date(state.ruleForm.createDate[0]), "YYYY-mm-dd HH:MM:SS"), formatDate(new Date(state.ruleForm.createDate[1]), "YYYY-mm-dd HH:MM:SS")].join();
    }


    if (sortsStr()) vo["sorts"] = sortsStr();
    let listTemp = await useFundApi().adminGetTransferHis(vo);

    let tempList = [] as any;
    listTemp.list.forEach((element: any) => {
        tempList.push(element)
    });

    state.dataList = tempList;

    state.total = listTemp.total;
}

const sortsStr = () => {
    let result = "";
    for (var prop in state.sorts) {
        result += prop + (state.sorts[prop] == "descending" ? " desc," : " asc,")
    }
    if (result == "") {
        return null;
    }
    return result.substring(0, result.length - 1)
}






const getcoinList = async () => {
    let listTemp = await useFundApi().getcoinList();
    state.coinList = listTemp;
}



// 页面加载时
onMounted(async () => {
    await getcoinList();
    adminGetTransferHis();
});



</script>





<style scoped lang="scss">
.bar_tools {
    display: flex;
    flex-direction: row-reverse;
    margin-bottom: 10px;
}

.system-offer-container {
    :deep(.el-card__body) {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;

        .el-table {
            flex: 1;
        }
    }
}


.table-search-container {
    display: flex;

    .table-form {
        flex: 1;

        .table-form-btn-toggle {
            white-space: nowrap;
            user-select: none;
            display: flex;
            align-items: center;
            color: var(--el-color-primary);
            margin-right: 15px;
        }
    }
}
</style>

